/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package csp;

import java.util.Set;

/**
 *
 * @author Leandro
 */
public class MVR implements HeuristicaVariable {

    public Variable getNextVariable(CSP problema) {

        int valoresRestantes=-1;
        //Variable nextVariable = null;
        int indice=-1;
        for (Variable v : problema.getVariables()) {
            if (v.getAsignado() == null) {
                if(valoresRestantes==-1){
                    valoresRestantes=v.getValoresPosibles().size();
                    indice=problema.getVariables().indexOf(v);
                }
                if (v.getValoresPosibles().size() < valoresRestantes) {
                    valoresRestantes = v.getValoresPosibles().size();
                    indice=problema.getVariables().indexOf(v);
                }
            }

        }
        if(valoresRestantes!= -1 && indice != -1){
            problema.setVariablePorAsignar(indice);
            return problema.getVariables().get(indice);
        }else{
        return null;
        }
    }

}
